<?php
namespace Models\Model;

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\Sql\Expression;
class SubjectsTable
{
    protected $tableGateway;
	protected $select;
    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
		$this->select = new Select();
    }
	
	public function saveSubjects($subject)
    {
		$data = array(
			'course_id' 		=> $subject['course_id'], 
			'subject_name' 	  	=> $subject['subject_name'], 	
			'status' 			=> 1, 				
		);
		
		$this->tableGateway->insert($data);	
		return $this->tableGateway->lastInsertValue;
    }
	public function updateSubjects($subject)
    {
		$data = array(
			'course_id' 		=> $subject['course_id'], 
			'subject_name' 	  	=> $subject['subject_name'],	
			'status' 			=> 1, 				
		);
		$updatedData=$this->tableGateway->update($data, array('subject_id' => $subject['SubId']));
		return 	$updatedData;
    }
	
	public function getSubjectList($course_id){
		$select = $this->tableGateway->getSql()->select();
		$select->join('pedia_courses', new Expression('pedia_courses.course_id='.$course_id.''),array('course_id','course_name'),'left');
		$select->where('pedia_subjects.course_id="'.$course_id.'"');
		$select->where('pedia_subjects.status="1"');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;
	}
	public function getSubject($subject_id,$course_id){
		$select = $this->tableGateway->getSql()->select();
		$select->join('pedia_courses', new Expression('pedia_courses.course_id=pedia_subjects.course_id'),array('course_id','course_name'),'left');
		$select->where('pedia_subjects.subject_id="'.$subject_id.'"');
		$select->where('pedia_subjects.status="1"');
		$select->where('pedia_subjects.course_id="'.$course_id.'"');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;
	}
	public function getTotalSubjectList(){
		$select = $this->tableGateway->getSql()->select();
		$select->join('pedia_courses', new Expression('pedia_courses.course_id=pedia_subjects.course_id'),array('course_id','course_name'),'left');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;
	}
	public function courseSubjects($course_id){
		$select = $this->tableGateway->getSql()->select();
		$select->where('course_id="'.$course_id.'"');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;
	}
	public function deleteSubject($sub_id)
    {	
	    $deleteStatus=$this->tableGateway->delete(array('subject_id' => $sub_id));
        return $deleteStatus;
	}
}